package controllers;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.mapping.Array;

import models.DriverJourney;
import models.Message;
import models.User;
import play.data.validation.Required;
import play.modules.paginate.ValuePaginator;
import play.mvc.With;

import common.FBSecure;

@With(FBSecure.class)
public class Messages extends FBController {

	public static void message(long id) {
		
		setFbIdandName();

		User user = getUser();
		
		User receiver = User.findById(id);
		
		render(user, receiver);
	}
	
	public static void sendmessage(long id, @Required String subject, @Required String message) {
		
		setFbIdandName();

		User user = getUser();
		
		User receiver = User.findById(id);
		
		user.addMessage(user, receiver, subject, message);
		
		List<Message> messages = user.getSentMessages();
		
		ValuePaginator paginator = new ValuePaginator(messages);
		paginator.setPageSize(10);
		
		renderTemplate("Messages/outbox.html", user, messages, paginator);
	}
	
	public static void inbox() {
		
		setFbIdandName();

		User user = getUser();
		
		List<Message> messages = user.getReceivedMessages();
		
		ValuePaginator paginator = new ValuePaginator(messages);
		paginator.setPageSize(10);
		
		render(user, messages, paginator);
	}
	
	public static void outbox() {
		
		setFbIdandName();

		User user = getUser();
		
		List<Message> messages = user.getSentMessages();
		
		ValuePaginator paginator = new ValuePaginator(messages);
		paginator.setPageSize(10);
		
		render(user, messages, paginator);
	}
	
	public static void inmessagedetails(long id) {
		
		setFbIdandName();

		User user = getUser();
		
		Message message = Message.findById(id);
		
		render(user, message);
	}
	
	public static void outmessagedetails(long id) {

		setFbIdandName();

		User user = getUser();
		
		Message message = Message.findById(id);
		
		render(user, message);
	}
	
	public static void deleteoutmessage(long id) {

		setFbIdandName();

		User user = getUser();
		
		Message message = Message.findById(id);
				
		message.delete();
		
		List<Message> messages = user.getSentMessages();
		
		ValuePaginator paginator = new ValuePaginator(messages);
		paginator.setPageSize(10);
		
		renderTemplate("Messages/outbox.html", user, messages, paginator);
	}
	
	public static void deleteinmessage(long id) {

		setFbIdandName();

		User user = getUser();
		
		Message message = Message.findById(id);
				
		message.delete();
		
		List<Message> messages = user.getReceivedMessages();
		
		ValuePaginator paginator = new ValuePaginator(messages);
		paginator.setPageSize(10);
		
		renderTemplate("Messages/inbox.html", user, messages, paginator);
	}
	
}
